<template>
  <!-- 主体区域 -->
  <section id="app">
    <!-- 输入框 -->
    <TodoHeader @add="addMsg"></TodoHeader>
    <!-- 列表区域 -->
    <TodoMain :list="list" @del="del"></TodoMain>
    <!-- 统计和清空 -->
    <TodoFooter :length="list.length" @clear="clear"></TodoFooter>
  </section>
</template>

<script>
import TodoHeader from './components/TodoHeader.vue'
import TodoMain from './components/TodoMain.vue'
import TodoFooter from './components/TodoFooter.vue'
export default {
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter
  },
  data() {
    return {
      list: JSON.parse(localStorage.getItem('list')) || [
        { id: 1, name: '打篮球' },
        { id: 2, name: '看电影' },
        { id: 3, name: '逛街' },
      ],
    }
  },
  watch: {
    list: {
      deep: true,
      handler(newValue) {
        localStorage.setItem('list', JSON.stringify(newValue))
      }
    }
  },
  methods: {
    addMsg(todoName) {
      //添加数据
      this.list.unshift({
        id: +new Date(),
        name: todoName
      })
    },
    del(id) {
      //过滤数据 -- 将不等于id的数据返回
      this.list = this.list.filter(item => item.id !== id)
    },
    clear() {
      this.list = []
    }
  },

}
</script>

<style>
</style>
